package com.asset.salesforce.dao;

import java.util.ArrayList;
import java.util.List;


import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.asset.salesforce.domain.Employee;
import com.asset.salesforce.domain.Login;

@Repository
public class EmployeeDaoImpl  {

	@Autowired
	SessionFactory sessionFactory;
	
	public void saveOrupdate(Employee employee){
		Session session = sessionFactory.getCurrentSession();
		session.saveOrUpdate(employee);
	}
	public void addEmployee(Employee employee) {
		try{
		sessionFactory.getCurrentSession().save(employee);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	public List<Employee> getAllEmplyee() {
		
		return sessionFactory.getCurrentSession().createQuery("from Employee").list();		
	}

	public void deleteEmployee(Integer id) {
		Employee employee = (Employee) sessionFactory.getCurrentSession().load(
				Employee.class, id);
		if (null != employee) {
			sessionFactory.getCurrentSession().delete(employee);
		}
		
	}
	public Employee loadEmployee(Integer id){
		Employee employee = (Employee)sessionFactory.getCurrentSession().get(Employee.class, id);
		return employee;
	}
	@SuppressWarnings("unchecked")
	public Employee getByEmailAndPassword(String email,String passowrd) {
		 Session session = sessionFactory.getCurrentSession();
		 Query query = session.createQuery("from Employee where email=  :email");
		 query.setParameter("email", email);
		List<Employee>list =   query.list();
		 for(Employee employee : list){
			 Query query1 = session.createQuery("from Login where empId = :empId");
			 query1.setParameter("empId", employee.getId());
 			 List<Login>  logins= query1.list();
 			 if(logins.size() >0){
 				 Login login = logins.get(0);
	 			 if(login.getPassword().equals(passowrd)){
	 				 return employee;
	 			 }
 			 }
		 }
		return null;
	}
	@SuppressWarnings("unchecked")
	public Employee getByEmail(String email) {
		 Session session = sessionFactory.getCurrentSession();
		 Query query = session.createQuery("from Employee where email=  :email");
		 query.setParameter("email", email.toLowerCase());
		 List<Employee>list  = new ArrayList<Employee>();
		 list = query.list();
		 if(list.size() >0){
			 return list.get(0);
		 }else{
			 return null;
		 }
		
	}
	
	@SuppressWarnings("unchecked")
	public Employee getById(Integer id) {
		 Session session = sessionFactory.getCurrentSession();
		 Query query = session.createQuery("from Employee where id=  :id");
		 query.setParameter("id", id);
		 List<Employee>list  = new ArrayList<Employee>();
		 list = query.list();
		 if(list.size() >0){
			 return list.get(0);
		 }else{
			 return null;
		 }
		
	}
	
	@SuppressWarnings("unchecked")
	public Employee getOwnerByCompanyName(String companyName){
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Employee where company.companyName= :companyName and isOwner= true");
		query.setParameter("companyName", companyName);
		List<Employee> emloyees =  query.list();
		if(emloyees.size()>0)
		{
			return emloyees.get(0);
		}else{
			return new Employee();
		}
	}
	
}
